define(function(){
    return var __base = {
        "init": function (modelArray) {
            var G = this;
            __base.common.call();
            //try {
            require(["allTpl", "mock"], function (template, Mock) {
                __base[modelArray[0]].call(G, template, Mock);
            });

            console.log(modelArray[0]);
            //} catch(error) {
            //console.error("body上找不到页面调用标示模块:", modelArray[0]);
            //console.error(error);
            //}

        },
        "common": function () {
            var host = _CONFIG[__webState]["AJAX_URL"];
            // 接口地址
            __base.url = {
                home: {
                    //轮播图和热门活动
                    "GetEPListBy": host + "api/MarketEP/GetEPListBy",
                    "AppPackageList": host + "api/AppPackage/AppPackageList",
                    "GetAllRecommend": host + "api/AppPackage/GetAllRecommend"
                },
                classPage: {
                    "CategoryList": host + "api/Category/CategoryList",
                    "Category2List": host + "api/Category/Category2List"
                },
                subClassPage: {
                    "GetPackageProductByPage": host + "api/AppPackage/GetPackageProductByPage"
                },
                goodsPage: {
                    "detail": host + "api/product/ProductDetail"
                },
                //购物车
                shoppingCar: {
                    //插入购物车
                    //  ProductSysNo: 1, //商品系统编码
                    //        Number:1, //商品数量
                    //        StockSysNo:1,//商品库存编码
                    //        ProductAttribute:"蓝色,3G"
                    "InsertShoppingCart": host + "api/ShoppingCart/InsertShoppingCart",
                    //获取购物车数量
                    "GetShopCartTotal": host + "api/ShoppingCart/GetShopCartTotal",
                    //删除购物车 sysNo: 1 //系统编码
                    "DeleteShoppingCart": host + "api/ShoppingCart/DeleteShoppingCart",
                    //更新购物车     //        sysNo: 1, //系统编码   number:12 //商品数量
                    "UpdateShoppingCart": host + "api/ShoppingCart/UpdateShoppingCart",
                    ///购物车列表
                    "GetShoppingCartList": host + "api/ShoppingCart/GetShoppingCartList",
                    "BatchUpdate": host + "api/ShoppingCart/BatchUpdate"
                },
                //商品列表
                "list": {
                    "search": host + "api/product/search"
                }
            };

            // 请求处理函数
            __base.AjaxFun = function (para, callback, error) {
                $.ajax({
                    url: para["url"],
                    type: "post",
                    //type: "get",
                    dataType: "json",
                    data: para["data"],
                    success: function (data) {
                        callback(data);
                    }
                }).fail(
                    function (xhr, textStatus, err) {
                        if (typeof (error) === "function") {
                            error(xhr, textStatus, err);
                        } else {
                            console.log(xhr, textStatus, err);
                        }
                    });
            };

        },
        "swiper": function (e) {
            require(["swiper"], function () {
                var options = $.extend(true, {
                    target: "",
                    loop: true,
                    autoplay: 5000,
                    lazyLoading: true,
                    lazyLoadingInPrevNext: true,
                    onTransitionStart: new Function
                }, e);

                console.log(options);
                return new Swiper(options.target, {
                    loop: true,
                    autoplay: options.autoplay,
                    lazyLoading: true,
                    lazyLoadingInPrevNext: true,
                    pagination: '.swiper-pagination',
                    onTransitionStart: options.onTransitionStart
                })
            })
        },
        //首页
        "home": function () {
            ajaxLoading.remove();
            require(["views/layouts/IndexPage.min"], function (home) {
                home.init()
            });
        },
        //分类查询页面
        "classPage": function (template, Mock) {

            var $contentClass_classPage = $("#contentClass_classPage");

            //加载一级分类
            new __base.AjaxFun({
                "url": __base.url.classPage.CategoryList,
                "data": {}
            }, function (data) {
                ajaxLoading.remove();
                if (data["Code"] === "0") {
                    var html = template('Reservation/classPage/subClass_classPage', data["Data"]);
                    var $subClass_classPage = $("#subClass_classPage");
                    $subClass_classPage.html(html);
                    $subClass_classPage.find('.itemBox').click(function () {
                        $(this).addClass("current").siblings().removeClass("current");
                        var dataSysno = $(this).attr("data-sysno");

                        require(["lazyload"], function (lazyload) {
                            /*加载二级分类*/
                            new __base.AjaxFun({
                                "url": __base.url.classPage.Category2List,
                                "data": {sysNo: dataSysno}
                            }, function (data2) {
                                if (data2["Code"] === "0") {
                                    $contentClass_classPage.scrollTop(0);

                                    var html2 = template('Reservation/classPage/contentClass_classPage', data2["Data"])
                                        .replace(/%URL%/g, _CONFIG[__webState].listPage_URL);
                                    $contentClass_classPage.html(html2);
                                    $(".lazyImg").lazyload({
                                        effect: "fadeIn",
                                        container: $contentClass_classPage,
                                        threshold: 150
                                    });
                                } else {
                                    alert(data2['Message']);
                                }
                            });
                        });


                    });
                    $subClass_classPage.find(".itemBox:eq(0)").trigger("click");

                } else {
                    console.log(data['Message']);
                }

            });

        },
        //子分类
        "subClassPage": function (template) {
            // 大图轮播
            new __base.AjaxFun({
                "url": __base.url.home.GetEPListBy,
                "data": {
                    ebCode: "APP_Ad_00003"
                }
            }, function (data) {
                ajaxLoading.remove();
                if (data["Code"] === "0") {
                    $("#subClassPage .swiper-wrapper").html(template('Reservation/subClassPage/swiper', data['Data']));
                    __base.swiper({target: ".swiper-container"},{
                        lazyLoading: true,
                        lazyLoadingInPrevNext: true
                    });

                } else {
                    alert(data['Message']);
                }
            });

            new __base.AjaxFun({
                "url": __base.url.subClassPage.GetPackageProductByPage,
                "data": {
                    sysNo: parse_url(window.location.href)['id'],
                    pageIndex: 1,
                    pageSize: 8
                }
            }, function (data) {
                if (data["Code"] === "0") {
                    var html = template('Reservation/subClassPage/list_subClassPage', data["Data"])
                        .replace(/%URL%/g, _CONFIG[__webState].goodsPage_URL);
                    $("#list_subClassPage").html(html);
                } else {
                    alert(data['Message']);
                }
            });
        },
        //商品详情
        "goodsPage": function (template, Mock) {
            var ProductSysNo = parse_url(window.location.href)['id'];

            var popWrap = new popF();


            new __base.AjaxFun({
                "url": __base.url.goodsPage.detail + "?productId=" + ProductSysNo,
                "data": {}
            }, function (data) {
                ajaxLoading.remove();
                if (data["Code"] === "0") {


                    console.log(data["Data"]);
                    //幻灯片
                    var swiper_html = template('Reservation/goodsPage/swiper', data["Data"])
                        .replace(/%URL%/g, _CONFIG[__webState].goodsPage_URL);
                    $("#goodsPage .swiper-wrapper").html(swiper_html);
                    //顶部图片切换
                    __base.swiper({target: ".swiper-container"});
                    // 商品主题信息
                    $("#intro_goodsPage").html(template('Reservation/goodsPage/intro_goodsPage', data["Data"]));

                    // 商品介绍 规格参数 包装售后
                    var $hd_goodsPage = $("#tab_goodsPage>.hd"),
                        $bd_goodsPage = $("#tab_goodsPage>.bd");

                    $bd_goodsPage.html(template('Reservation/goodsPage/tab_goodsPage', data['Data']));

                    $hd_goodsPage.on('tap', 'li', function (e) {
                        $(this).addClass("current").siblings().removeClass("current");
                        $bd_goodsPage.find("li").eq($hd_goodsPage.find("li").index(this)).show().siblings().hide();
                    });

                    //加入购物车
                    var $footerBox_Global = $('.footerBox_Global'),
                        addShoppingCar = function (callback) {
                            var num = $footerBox_Global.find(".count").text();
                            new __base.AjaxFun({
                                "url": __base.url.shoppingCar.InsertShoppingCart,
                                "data": {
                                    ProductSysNo: ProductSysNo, //商品系统编码
                                    Number: $('.countForOne_Global .value_cfo').val(), //商品数量
                                    StockSysNo: data["Data"]["stock"] && data["Data"]["stock"][0]["stock_sysNo"],//商品库存编码
                                    ProductAttribute: data["Data"]["attributes"][0]["values"][0]["Name"], //"蓝色,3G"
                                    PriceSysNo: data["Data"]["price"]["PriceSysNo"], //商品价格编码
                                    CurrentPrice: data["Data"]["price"]["currentPrice"], //如果页面上输入了 当前价格则传页面值 否则传商品价格的CurrentPrice
                                    CurrentScore: data["Data"]["price"]["currentPoint"] //如果页面上输入了 当前积分则传页面值 否则传商品积分的CurrentScore
                                }
                            }, function (data_InsertShoppingCart) {
                                ajaxLoading.remove();
                                if (data["Code"] === "0") {
                                    callback();
                                    getShopingCartNum();
                                } else {
                                    popWrap.defult({
                                        content: data_InsertShoppingCart['Message'],
                                        btnNum: 1,
                                        btn_r: "知道了"
                                    });
                                }
                            });
                        };



                    $footerBox_Global.on('click', '.md', function () {
                        addShoppingCar(function () {
                            popWrap.alert({content:"添加成功"});
                        })
                    });


                    //点击立即购买
                    $footerBox_Global.on('click', '.rd', function () {
                        addShoppingCar(function () {
                            window.location.href = _CONFIG[__webState].shoppingCarPage_URL;
                        });
                    });


                    // 商品数量
                    var $value_cfo = $('.value_cfo'),
                        dataStock = data["Data"]["stock"][0],
                        dataStockNum = dataStock["quanity"] - dataStock["freeze_quanity"]; // 可用库存

                    $('.remove_cfo').click(function () {
                        if (Number($value_cfo.val()) <= 1) {
                            return false
                        }
                        var ls = Number($value_cfo.val()) - 1;
                        reChange(ls);
                    });
                    $('.add_cfo').click(function () {
                        var ls = Number($value_cfo.val()) + 1;

                        reChange(ls);
                    });

                    $value_cfo.on({
                        "keyup":function(){
                            $(this).val($(this).val().replace(/\D/g,""));
                            reChange(Number($value_cfo.val()));
                        },
                        "blur":function(){

                        }
                    });

                    //  已选多少件
                    function reChange(ls) {
                        if (ls > dataStockNum){
                            popWrap.alert({content:"商品库存不足！",autoClose:1000});
                            return false;
                        }
                        $value_cfo.val(ls);
                        $('#tools_goodsPage .num_cfo').text($value_cfo.val());
                    }



                } else {
                    console.log(data['Message']);
                }
            });

            //购物车
            // 获取购物车数量
            function getShopingCartNum(){
                new __base.AjaxFun({
                    "url": __base.url.shoppingCar.GetShopCartTotal,
                    "data": {}
                }, function (data) {
                    if (data["Code"] === "0") {
                        $('.footerBox_Global .count').text(data["Data"]);
                    } else {
                        console.log(data['Message']);
                    }
                });
            };
            getShopingCartNum();




        },
        // 商品列表页
        "listPage": function () {
            require(["views/layouts/listPage.min"], function (app) {
                app.init()
            });
        },
        //购物车
        "shoppingCarPage": function (template, Mock) {

            require(["views/layouts/shoppingCarPage.min"], function (app) {
                app.init()
            });


            //$("#goodsPage .swiper-wrapper").html(template('Reservation/goodsPage/swiper', data["Data"]));
        },
        //填写订单
        "fillOrderPage": function(template, Mock){
            require(["views/layouts/fillOrderPage.min","page","validate"], function (app) {
                app.init()
            });
        }


    };
})